﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace paipai
{
    public partial class HomePage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //var d = Bll.CPicsManage.CreatePics();
            //d.ID = Guid.NewGuid().ToString();
            //d.imgID = "a";
            //d.imgURL = "../testPics/2.jpg";
            //Bll.CPicsManage.Save(d);
            #region   session check & init
            if (!IsPostBack)
            {
                if (Session["UserID"] != null)
                {
                    ViewState.Add("UserID", Session["UserID"]);
                }
            }
            #endregion
            if (!IsPostBack)
            {
                BindData();
 
            }
            //GridView1.DataSource = Bll.CPicsManage.LoadAllPics();
            //GridView1.DataBind();
            ////GridView2.DataSource =
           // Page.DataBind();
        }

        private void BindData()
        {
            int i;
            int countPerRow = 5;
            List<Bll.CPics> images = Bll.CPicsManage.LoadAllPics();
            int size = images.Count;
            DataSet ds = new DataSet("ds_dry");
            DataTable dt = new DataTable("dt_dry");
            for (i = 0; i < countPerRow; i++)
            {
                dt.Columns.Add(new DataColumn("imgURL" + i, typeof(System.String)));
            }
            DataRow dr;
            int j = 0;
            while (j < size)
            {
                dr = dt.NewRow();
                i = 0;
                while (j < size && i < countPerRow)
                {
                    dr["imgURL" + j % countPerRow] = images[i].imgURL;
                    j++;
                    i++;
                }
                if (j >= size)
                {
                    dt.Rows.Add(dr);
                    break;
                }
                dt.Rows.Add(dr);
            }

            ds.Tables.Add(dt);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
        }
    }
}